package com.joysuch.wwyt.risk.controller;

import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.constant.Constants;
import com.joysuch.wwyt.risk.bean.*;
import com.joysuch.wwyt.risk.entity.RiskDynamicConfig;
import com.joysuch.wwyt.risk.entity.RiskEvaluationAnalyzeWay;
import com.joysuch.wwyt.risk.entity.RiskEvaluationControl;
import com.joysuch.wwyt.risk.entity.RiskEvaluationListField;
import com.joysuch.wwyt.risk.entity.dto.RiskDynamicConfigDto;
import com.joysuch.wwyt.risk.entity.vo.RiskDynamicConfigBatchSaveVo;
import com.joysuch.wwyt.risk.repository.RiskEvaluationControlDao;
import com.joysuch.wwyt.risk.service.RiskEvaluationTempleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/risk/evaluation")
@Api(tags = "双预防-评估模板")
@Slf4j
public class RiskEvaluationTempleController {

    @Autowired
    private RiskEvaluationTempleService service;

    @Resource
    private RiskEvaluationControlDao riskEvaluationControlDao;

    @GetMapping("editSwitchState")
    @ApiOperation("设置基础配置开关状态")
    public ResultBean editSwitchState(String name, Integer state) {
        RiskEvaluationControl ct = service.editSwitchState(name, state);
        return ResultBean.success(ct);
    }

    @GetMapping("getControlList")
    @ApiOperation("获取基础配置列表")
    public ResultBean getControlList() {
        List<RiskEvaluationControlBean> ct = service.getControlList();
        return ResultBean.success(ct);
    }


    @PostMapping("saveCorrectionRuleList")
    @ApiOperation("批量保存校正规则")
    @RequiresPermissions("risk:evaluation:saveCorrectionRuleList")
    public ResultBean saveCorrectionRuleList(@RequestBody List<RiskEvaluationCorrectionRuleBean> listBean) {
        service.saveCorrectionRuleList(listBean);
        return ResultBean.success(listBean);
    }

    @GetMapping("getCorrectionRuleList")
    @ApiOperation("获取校正规则列表")
    public ResultBean getCorrectionRuleList() {
        List<RiskEvaluationCorrectionRuleBean> list = service.getCorrectionRuleList();
        return ResultBean.success(list);
    }

    @GetMapping("deleteCorrectionRule")
    @ApiOperation("删除校正规则")
    @RequiresPermissions("risk:evaluation:deleteCorrectionRule")
    public ResultBean deleteCorrectionRule(Long id) {
        service.deleteCorrectionRule(id);
        return ResultBean.success("");
    }

    @PostMapping("saveListField")
    @ApiOperation("保存管控清单字段")
    @RequiresPermissions("risk:evaluation:saveListField")
    public ResultBean saveListField(@RequestBody RiskEvaluationListFieldBean bean) {
        RiskEvaluationListField field = service.saveListField(bean);
        return ResultBean.success(field);
    }

    @GetMapping("deleteListField")
    @ApiOperation("删除管控清单字段")
    @RequiresPermissions("risk:evaluation:deleteListField")
    public ResultBean deleteListField(Long id) {
        service.deleteListField(id);
        return ResultBean.success("");
    }

    @GetMapping("getFieldList")
    @ApiOperation("获取管控清单字段列表")
    public ResultBean getFieldList() {
        List<RiskEvaluationListFieldBean> list = service.getFieldList();
        return ResultBean.success(list);
    }

    @GetMapping("getSelectFieldList")
    @ApiOperation("获取管控清单字段下拉列表")
    public ResultBean getSelectFieldList() {
        List<SimpleSelectListBean> list = service.getSelectFieldList();
        return ResultBean.success(list);
    }


    @GetMapping("editFieldOrder")
    @ApiOperation("设置管控清单字段顺序")
    @RequiresPermissions("risk:evaluation:editFieldOrder")
    public ResultBean editFieldOrder(Long upId, Long downId) {
        service.editFieldOrder(upId, downId);
        return ResultBean.success("");
    }

    @PostMapping("saveAnalyzeWay")
    @ApiOperation("保存方法配置")
    @RequiresPermissions("risk:evaluation:saveAnalyzeWay")
    public ResultBean saveAnalyzeWay(@RequestBody RiskEvaluationAnalyzeWayBean bean) {
        RiskEvaluationAnalyzeWay way = service.saveAnalyzeWay(bean);
        return ResultBean.success(way);
    }

    @GetMapping("findAnalyzeWay")
    @ApiOperation("查看方法配置")
    public ResultBean findAnalyzeWay(Long wayId) {
        RiskEvaluationAnalyzeWayBean bean = service.findAnalyzeWay(wayId);
        return ResultBean.success(bean);
    }

    @GetMapping("deleteAnalyzeWay")
    @ApiOperation("删除方法配置")
    @RequiresPermissions("risk:evaluation:deleteAnalyzeWay")
    public ResultBean deleteAnalyzeWay(Long id) {
        service.deleteAnalyzeWay(id);
        return ResultBean.success("");
    }

    @GetMapping("getAllAnalyzeWay")
    @ApiOperation("获取配置方法列表")
    public ResultBean getAllAnalyzeWay(Integer analyzeType) {
        List<SimpleSelectListBean> list = service.getAllAnalyzeWay(analyzeType);
        return ResultBean.success(list);
    }

    @GetMapping("editWayRelation")
    @ApiOperation("使用评估方法")
    @RequiresPermissions("risk:evaluation:editWayRelation")
    public ResultBean editWayRelation(Long analyzeId, Long evaluationId) {
        RiskEvaluationAnalyzeWay way = service.editWayRelation(analyzeId, evaluationId);
        return ResultBean.success(way);
    }

    @PostMapping("saveEvaluetionTarget")
    @ApiOperation("保存评估指标")
    @RequiresPermissions("risk:evaluation:saveEvaluetionTarget")
    public ResultBean saveEvaluetionTarget(@RequestBody List<RiskEvaluationTargetTypeBean> list) {
        service.saveEvaluetionTarget(list);
        return ResultBean.success("");
    }

    @GetMapping("getTargetList")
    @ApiOperation("获取评估指标")
    public ResultBean getTargetList(Long methodId) {
        return service.getTargetList(methodId);
    }


    @PostMapping("dynamicConfigSave")
    @ApiOperation("保存动态风险评估配置")
    public ResultBean dynamicRiskSave(@RequestBody RiskDynamicConfigBatchSaveVo vo) {
        List<RiskDynamicConfig> configs = service.dynamicRiskSave(vo);
        return ResultBean.success(configs);
    }

    @GetMapping("getDynamicConfig")
    @ApiOperation("查看动态风险评估配置")
    public ResultBean getDynamicConfig() {
        List<RiskDynamicConfigDto> beanList = service.getDynamicConfig();
        return ResultBean.success(beanList);
    }

    @GetMapping("getDynamicConfigByName")
    @ApiOperation("查询未检任务触发动态风险配置")
    public ResultBean getDynamicConfigByName() {
        RiskEvaluationControl control = riskEvaluationControlDao.findByName(Constants.UNCHECKED_TASK_TRIGGER_DYNAMIC_RISK);
        return ResultBean.success(control != null && control.getSwitchState() != null ? control.getSwitchState() : 1);
    }

    @GetMapping("getFormJsonByProcessId")
    @ApiOperation("获取动态风险评估配置组件formJson")
    public ResultBean getFormJsonByProcessId(Long processId) {
        List<RiskDyncmicOptionTypeBean> dataList = service.getFormJsonByProcessId(processId);
        return ResultBean.success(dataList);
    }
}
